package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ContractInvoice;
import net.hn.hnms.biz.project.domain.ContractInvoicePayment;
import net.hn.hnms.biz.project.domain.ContractPayment;
import net.hn.hnms.biz.project.mapper.ContractInvoiceMapper;
import net.hn.hnms.biz.project.mapper.ContractInvoicePaymentMapper;
import net.hn.hnms.biz.project.request.ContractInfoReq;
import net.hn.hnms.biz.project.request.ContractInvoiceReq;
import net.hn.hnms.biz.project.service.ContractInvoiceService;
import net.hn.hnms.biz.project.vo.ContractInfoVo;
import net.hn.hnms.biz.project.vo.ContractInvoiceVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContractInvoiceServiceImpl implements ContractInvoiceService {
    @Autowired
    private ContractInvoiceMapper contractInvoiceMapper;

    @Autowired
    private ContractInvoicePaymentMapper contractInvoicePaymentMapper;

    @Override
    @Transactional
    public int insert(ContractInvoiceReq contractInvoiceReq) {
        ContractInvoice insertObj = new ContractInvoice();
        BeanUtil.copyProperties(contractInvoiceReq, insertObj);
        int flag = contractInvoiceMapper.insert(insertObj);
        String contractPaymentId = null;
        contractPaymentId = contractInvoiceReq.getContractPaymentId();
        if(contractPaymentId != null){
            ContractInvoicePayment pay = new ContractInvoicePayment();
            pay.setInvoiceId(insertObj.getContractInvoiceId());
            pay.setPaymentId(contractPaymentId);
            contractInvoicePaymentMapper.insert(pay);
        }
        return flag;
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        contractInvoiceMapper.updateDeleteByIds(idList);
    }

    @Override
    @Transactional
    public void updateById(ContractInvoiceReq contractInvoiceReq) {
        ContractInvoice updateObj = new ContractInvoice();
        BeanUtil.copyProperties(contractInvoiceReq, updateObj);
        int updateCount = contractInvoiceMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        contractInvoicePaymentMapper.updateContractInvoicePayment(contractInvoiceReq);
    }

    @Override
    public ContractInvoice selectDetailById(String id) {
        ContractInvoice contractInvoice = contractInvoiceMapper.selectById(id);
        if (contractInvoice == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return contractInvoice;
    }

    @Override
    public ResponsePage<ContractInvoiceVo> selectPageList(ContractInvoiceReq contractInvoiceReq, PageQuery pageQuery) {
        Page<ContractInvoiceVo> page = contractInvoiceMapper.selectPage(pageQuery.build(), contractInvoiceReq);
//        Page<ContractInvoice> page = contractInvoiceMapper.selectPage(pageQuery.build(), buildQuery(contractInvoiceReq).orderByDesc(ContractInvoice::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public ResponsePage<ContractInfoVo> selectContractPageList(ContractInfoReq contractInfoReq, PageQuery pageQuery) {
        Page<ContractInfoVo> page = contractInvoiceMapper.selectContractPageList(pageQuery.build(), contractInfoReq);
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<ContractPayment> getPayList(ContractInvoiceReq ContractInvoiceReq) {
        return contractInvoiceMapper.getPayList(ContractInvoiceReq);
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ContractInvoice> buildQuery(ContractInvoiceReq req){
        return new LambdaQueryWrapper<ContractInvoice>()
                .like(StrUtil.isNotBlank(req.getInvoiceCompany()), ContractInvoice::getInvoiceCompany, req.getInvoiceCompany())
                .eq(StrUtil.isNotBlank(req.getContractId()), ContractInvoice::getContractId, req.getContractId());
    }
}
